/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unioeste.cc.vm;

import java.util.ArrayList;

/**
 *
 * @author diego
 */
public class MemoryInstruction {

    ArrayList<String> memoryRead;
    Memory memory;
    String inst;
   	int pc;
//    int typeInstruction;
    int opcode, rs, rt, rd, shamt, funct;
    int adress;
    int Constante;
    
    String arquivo;
 public int getConstante() {
      return Constante;
}

public void setConstante(int constante) {
      Constante = constante;
}
    public String getArquivo() {
        return arquivo;
    }

    public void setArquivo(String arquivo) {
        this.arquivo = arquivo;
    }

    public int getFunct() {
        return funct;
    }

    public void setFunct(int funct) {
        this.funct = funct;
    }

    public int getOpcode() {
        return opcode;
    }

    public void setOpcode(int opcode) {
        this.opcode = opcode;
    }

    public int getRd() {
        return rd;
    }

    public void setRd(int rd) {
        this.rd = rd;
    }

    public int getRs() {
        return rs;
    }

    public void setRs(int rs) {
        this.rs = rs;
    }

    public int getRt() {
        return rt;
    }

    public void setRt(int rt) {
        this.rt = rt;
    }

    public int getShamt() {
        return shamt;
    }

    public void setShamt(int shamt) {
        this.shamt = shamt;
    }

    public int getPc() {
        return pc;
    }

    public void setPc(int pc) {
        this.pc = pc;
    }

    public String getInst() {
        return inst;
    }

    public void setInst(String inst) {
        this.inst = inst;
    }

    public Memory getMemory() {
        return memory;
    }

    public void setMemory(Memory memory) {
        this.memory = memory;
    }

    public int getAdress() {
        return adress;
    }

    public void setAdress(int adress) {
        this.adress = adress;
    }

    public ArrayList<String> getMemoryRead() {
        return memoryRead;
    }

    public void setMemoryRead(ArrayList<String> memoryRead) {
        this.memoryRead = memoryRead;
    }

    public MemoryInstruction(String path) {
        Memory memory = new Memory();
        this.memoryRead = memory.loadMemory(path);
    }

    public ArrayList<String> getMemoryread() {
        return memoryRead;
    }

    public void setMemoryread(ArrayList<String> memoryread) {
        this.memoryRead = memoryread;
    }

    public void searchInstruction() {
        inst = getMemoryRead().get(pc / 4);
    }

    public void instructionType() {
        opcode = Integer.parseInt(inst.substring(0, 6),2);
        switch (opcode) {
            case 0://formato R
                rs = Integer.parseInt(inst.substring(6, 11),2);
                rt = Integer.parseInt(inst.substring(11, 16),2);
                rd = Integer.parseInt(inst.substring(16, 21),2);
                shamt = Integer.parseInt(inst.substring(21, 26),2);
                funct = Integer.parseInt(inst.substring(26, 32),2);
//                System.out.println("\n" + "Instrução do Tipo-R: \n");
//                System.out.println("opcode = " + opcode);
//                System.out.println("rs = " + rs);
//                System.out.println("rt = " + rt);
//                System.out.println("rd = " + rd);
//                System.out.println("shamt = " + shamt);
//                System.out.println("funct = " + funct + "\n");
                break;
            //Formato I    
            case 4: //beq 
                     break;
            case 5: //bne
                break;
            case 15: //lui
                   rt=Integer.parseInt(inst.substring(16,20),2);
                   Constante=Integer.parseInt(inst.substring(0,15),2);
                break;
            case 35: //lw
                break;
            case 43: //sw
                rs = Integer.parseInt(inst.substring(6, 11),2);
                rt = Integer.parseInt(inst.substring(11, 16),2);
                adress = Integer.parseInt(inst.substring(16,32),2);
            //Formato J
            case 2://jump
                adress = Integer.parseInt(inst.substring(6,32),2);                 
            default:
                break;
        }
    }
}
